package com.hunttown.mes.manage.service;

import com.hunttown.mes.rpc.domain.WorkflowBusinessDTO;
import com.hunttown.common.domain.Page;
import com.hunttown.mes.common.utils.FormatUtils;
import com.hunttown.mes.rpc.api.WorkflowBusinessNodeRpcService;
import com.hunttown.mes.rpc.domain.WorkflowBusinessNodeDTO;
import com.hunttown.mes.rpc.domain.query.WorkflowBusinessNodeDTOQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * created by wangjunfu on 2019-08-01 16:55:26
 */
@Service
public class WorkflowBusinessNodeManageService {

    private final static Logger logger = LoggerFactory.getLogger(WorkflowBusinessNodeManageService.class);

    private final WorkflowBusinessNodeRpcService rpcService;
    private final WorkflowBusinessManageService businessService;
    private final TransactionService transactionService;

    @Autowired
    public WorkflowBusinessNodeManageService(WorkflowBusinessNodeRpcService rpcService, WorkflowBusinessManageService businessService, TransactionService transactionService) {
        this.rpcService = rpcService;
        this.businessService = businessService;
        this.transactionService = transactionService;
    }

    // 新增
    public WorkflowBusinessNodeDTO insert(WorkflowBusinessNodeDTO objDTO) {
        return rpcService.insert(objDTO);
    }

    // 通过ID修改
    public Boolean updateInfoById(WorkflowBusinessNodeDTO objDTO) {
        return rpcService.updateInfoById(objDTO);
    }

    // 通过Query修改
    public Boolean updateInfoByQuery(WorkflowBusinessNodeDTOQuery objDTOQuery) {
        return rpcService.updateInfoByQuery(objDTOQuery);
    }

    // 通过ID删除
    public Boolean delete(Integer id) {
        return rpcService.deleteById(id);
    }

    // 批量删除
    public Boolean deleteForBatch(String clearIds) {

        //处理 “,5,6,7,” 为List
        List<Integer> idList = FormatUtils.commaToList_Integer(clearIds);

        if (idList.size() == 0) {
            return false;
        }

        WorkflowBusinessNodeDTOQuery query = new WorkflowBusinessNodeDTOQuery();
        query.setDeleteFlag(1);
        query.setIds(idList);

        return rpcService.updateInfoByQuery(query);
    }

    // 通过ID获取
    public WorkflowBusinessNodeDTO getById(Integer id) {
        if (id <= 0) {
            return null;
        }
        return rpcService.selectById(id);
    }

    // 通过Query获取
    public WorkflowBusinessNodeDTO getByQuery(WorkflowBusinessNodeDTOQuery objDTOQuery) {
        return rpcService.selectObjByQuery(objDTOQuery);
    }

    // 获取分页列表
    public Page<WorkflowBusinessNodeDTO> getForPage(WorkflowBusinessNodeDTOQuery objDTOQuery) {
        return rpcService.getListForPage(objDTOQuery);
    }

    // 改变某条记录的使用状态
    public Boolean changeState(Integer id, Integer state) {
        WorkflowBusinessNodeDTO obj = new WorkflowBusinessNodeDTO();
        obj.setStopFlag(state);
        obj.setId(id);
        return updateInfoById(obj);
    }

    //region 判断各业务的条件审批

    /**
     * 判断各业务的条件审批
     *
     * @param businessName 审批流程英文名称
     * @param judgeType    比较类型：1传入值大于设定值则拦截；-1传入值小于设定值则拦截
     * @param dataValue    比较数值
     * @param applyId      审批ID
     */
    public void handleJudgeApply(String businessName, Integer judgeType, BigDecimal dataValue, Integer applyId) {

        //region STEP 01.获取审批流程
        WorkflowBusinessDTO business = businessService.getByEnName(businessName);
        if (business == null) {
            return;
        }
        //endregion

        //region STEP 02.获取所有的条件判断环节
        WorkflowBusinessNodeDTOQuery query = new WorkflowBusinessNodeDTOQuery();
        query.setBusinessId(business.getId());
        query.setIsJudge(1);
        query.setStopFlag(0);
        Page<WorkflowBusinessNodeDTO> nodePage = getForPage(query);
        //endregion

        //region STEP 03.根据设定值判断是手动还是自动审批
        for (WorkflowBusinessNodeDTO item : nodePage.getItems()) {
            if (dataValue.compareTo(item.getJudgeData()) == judgeType) {
                transactionService.setManageApply(applyId, item.getNodeSort(), 0);  //手动审批
            } else {
                transactionService.setManageApply(applyId, item.getNodeSort(), 1);  //自动审批
            }
        }
        //endregion
    }
    //endregion
}